package view.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class NodesRenderer extends JPanel implements MouseMotionListener,
		MouseListener {

	private static final long serialVersionUID = 3751578887004822242L;
	public List<NodeView> nodes = new ArrayList<NodeView>();
	private double scale = 1;
	private static JTextArea output;

	public double getScale() {
		return scale;
	}

	public void setScale(double scale) {
		this.scale = scale;
	}

	private static void createOutputWindow(JFrame frame) {
		output = new JTextArea();
		output.setLineWrap(true);
		frame.add(output, BorderLayout.SOUTH);
	}

	public final static void main(String arguments[]) {
		JFrame frame = new JFrame("test");
		frame.setLayout(new BorderLayout());
		NodesRenderer panel = new NodesRenderer();
		frame.add(panel, BorderLayout.CENTER);
		frame.setPreferredSize(new Dimension(800, 600));
		createOutputWindow(frame);
		frame.pack();
		frame.setVisible(true);
	}

	public NodesRenderer() {
		this.setLayout(null);
		this.addMouseMotionListener(this);
		this.addMouseListener(this);
	}

	private void drawLink(Graphics2D graphics, NodeView sourceNode,
			NodeView targetNode) {

		int sourceCenterX = sourceNode.getX() + (sourceNode.getWidth() / 2);
		int sourceCenterY = sourceNode.getY() + (sourceNode.getHeight() / 2);

		int targetCenterX = targetNode.getX() + (targetNode.getWidth() / 2);
		int targetCenterY = targetNode.getY() + (targetNode.getHeight() / 2);

		int verticalArrowX[] = { sourceCenterX - 10, sourceCenterX + 10,
				targetCenterX + 2, targetCenterX - 2 };
		int verticalArrowY[] = { sourceCenterY, sourceCenterY, targetCenterY,
				targetCenterY };

		int horizontalArrowX[] = { sourceCenterX, sourceCenterX, targetCenterX,
				targetCenterX };
		int horizontalArrowY[] = { sourceCenterY - 10, sourceCenterY + 10,
				targetCenterY + 2, targetCenterY - 2 };

		graphics.setColor(Color.RED);
		graphics.fillPolygon(verticalArrowX, verticalArrowY, 4);
		graphics.fillPolygon(horizontalArrowX, horizontalArrowY, 4);

		graphics.drawLine(sourceCenterX, sourceCenterY, targetCenterX,
				targetCenterY);
	}

	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D graphics = (Graphics2D) g;
		graphics.scale(scale, scale);
		super.paintComponent(graphics);
		for (int i = 1; i < nodes.size(); i++) {
			NodeView sourceNode = nodes.get(i - 1);
			NodeView targetNode = nodes.get(i);
			drawLink(graphics, sourceNode, targetNode);
		}
	}

	private static int count = 0;

	@Override
	public void mouseDragged(MouseEvent event) {
	}

	@Override
	public void mouseMoved(MouseEvent event) {

	}

	@Override
	public void mouseClicked(MouseEvent event) {
		NodeView node = new NodeView(this);
		Dimension size = new Dimension(80, 120);
		Insets insets = this.getInsets();
		node.setPreferredSize(size);
		node.setName("ID " + count++);
		Point point = event.getPoint();
		node.setBounds((int) (point.x / scale) + insets.left,
				(int) (point.y / scale) + insets.top, size.width, size.height);
		this.nodes.add(node);
		this.add(node);
		this.setComponentZOrder(node, 0);
		this.validate();
		repaint();
	}

	@Override
	public void mouseEntered(MouseEvent event) {

	}

	@Override
	public void mouseExited(MouseEvent event) {

	}

	@Override
	public void mousePressed(MouseEvent event) {

	}

	@Override
	public void mouseReleased(MouseEvent event) {

	}

}
